/**
 * Title: RoleController.java
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.controller.security;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.CommonException;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.po.security.Group;
import com.pm.service.security.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Title: RoleController
 * @author wujin
 * @date 2019年12月30日
 */
@Api(tags = "群组管理")
@ApiModel
@UserLoginToken
@RestController
@RequestMapping("/group")
public class GroupController {

    @Autowired
    private GroupService groupService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增群组信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> add(@RequestBody Group group) {
        Response<Integer> response = null;
        Integer result = groupService.add(group);
        if (result > 0) {
            List<Integer> groupIds = new ArrayList<Integer>();
            for (String id : group.getPermissionIds().split(",")) {
                if (StringUtils.isNumber(id)) {
                    groupIds.add(Integer.parseInt(id));
                }
            }
            groupService.insertBatch(group.getGroupId(), groupIds);
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    /**
     * Title: roleAllocation
     * @param roleId
     * @param permisssion
     * @return
     */
    @ApiOperation("分配群组接口")
    @PostMapping("/groupAllocation")
    @ResponseBody
    Response<Integer> groupAllocation(@RequestParam Integer groupId, @RequestParam String permissions) {
        List<Integer> permissionIds = new ArrayList<Integer>();
        for (String id : permissions.split(",")) {
            if (StringUtils.isNumber(id)) {
                permissionIds.add(Integer.parseInt(id));
            }
        }
        Response<Integer> response = new Response<>(ExpCodeState.add_data_success, groupService.insertBatch(groupId, permissionIds));
        return response;
    }

    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑群组信息")
    @Transactional(rollbackFor = CommonException.class)
    public Response<Integer> update(Group group) {
        Response<Integer> response = null;
        Integer result = groupService.update(group);
        List<Integer> groupIds = new ArrayList<Integer>();
        for (String id : group.getPermissionIds().split(",")) {
            if (StringUtils.isNumber(id)) {
                groupIds.add(Integer.parseInt(id));
            }
        }
        groupService.insertBatch(group.getGroupId(), groupIds);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除群组信息")
    public Response<Integer> remove(Group group) {
        Response<Integer> response = null;
        Integer result = groupService.remove(group);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<Group> load(Group group) {
        Response<Group> response = null;
        Group result = groupService.load(group);
        response = new Response<Group>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<Group>> getList(Group group) {
        List<Group> result = groupService.getList(group);
        Response<List<Group>> res = new Response<List<Group>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询群组信息")
    public Response<PageInfo<Group>> getList(Group group, Pages page) {
        PageInfo<Group> result = groupService.getListByPage(group, page);
        Response<PageInfo<Group>> res = new Response<PageInfo<Group>>(ExpCodeState.query_data_success, result);
        return res;
    }
}
